/*
 * LogEx a Java library for extracting log file data
 * Copyright 2012 LogEx Team.
 *
 * This program is free software. You can distribute or modify it under the 
 * terms of the GNU Lesser General Public License version 3.0 as published by  
 * the Free Software Foundation.
 *
 * This program is distributed AS IS and WITHOUT WARRANTY. OF ANY KIND,
 * INCLUDING MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
 * See the GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program; if not, contact the Free Software Foundation, Inc.,
 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 */
package org.logex.api;

import java.io.FileNotFoundException;
import java.io.IOException;

import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.xquery.XQException;
import org.ikayzo.sdl.SDLParseException;
import org.logex.main.LocalLogReader;
import org.logex.main.XmlParser;
import org.logex.utils.RemoteFile;
import org.xml.sax.SAXException;

import com.jcraft.jsch.JSchException;

/**
 * Handles method calls for reading ASCII and XML log files.
 * 
 * @author Lohitha Chiranjeewa
 */
public class DataReader {

	private LocalLogReader localLogReader;
	private XmlParser xmlParser;
	private RemoteFile remoteFile;

	/**
	 * Calls the main script reading method of the <code>LocalLogReader</code>
	 * class.
	 * 
	 * @param logFile
	 *            Name (with path) of the log file
	 * @param sdlFile
	 *            Name of the corresponding SDL file (no path needed, file needs
	 *            to be put into SDL folder)
	 * @throws IOException
	 *             Errors related to files
	 * @throws XQException
	 *             Database errors
	 * @throws SDLParseException
	 *             If the SDL format is malformed
	 */
	public void readAsciiLogFile(String logFile, String sdlFile)
			throws IOException, XQException, SDLParseException {
		localLogReader= new LocalLogReader(logFile, sdlFile);
		localLogReader.readScript();
	}

	/**
	 * Calls the methods in XmlParser class.
	 * 
	 * @param logFileName
	 *            Name of the XML document with the location.
	 * @throws TransformerException
	 *             If XML transformation error occurs
	 * @throws SAXException
	 *             If a general SAX error or warning occurs
	 * @throws ParserConfigurationException
	 *             If a XML parser configuration error occurs
	 * @throws XQException
	 *             If reasons such as wrong commands, DB errors occur
	 * @throws IOException
	 *             Due to errors in database reading
	 * @throws FileNotFoundException
	 *             Occurs if the file denoted by a specified pathname has failed
	 * 
	 * @throws TransformerConfigurationException
	 *             If specifies an exceptional condition that occur during the
	 *             transformation process.
	 */
	public void readXmlLogFile(String logFileName)
			throws TransformerConfigurationException, IOException, XQException,
			ParserConfigurationException, SAXException, TransformerException {
		xmlParser = new XmlParser(logFileName);
		xmlParser.getXmlFile();
	}

	/**
	 * Calls the main script reading method of the <code>LocalLogReader</code>
	 * class.
	 * 
	 * @param remoteLogFilePath
	 *            Name (with path) of the log file
	 * @param sdlFile
	 *            Name of the corresponding SDL file (no path needed, file needs
	 *            to be put into SDL folder)
	 * @throws IOException
	 *             Errors related to files
	 * @throws XQException
	 *             Database errors
	 * @throws SDLParseException
	 *             If the SDL format is malformed
	 * @throws JSchException
	 * 				Errors happens when creating the session with the remote host
	 * */
	public void readRemoteAsciiLogFile(String remoteLogFilePath, String sdlFile) throws IOException, SDLParseException, JSchException, XQException {
		remoteFile = new RemoteFile(remoteLogFilePath, sdlFile);
		remoteFile.readRemoteFile();
	}
}
